<?php
/**
 * sudokufrm.php
 * @abstract SudokuMatrix example AJAX form
 * @author Manel Zaera (manelzaera@gmail.com)
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
 *
 * Changes:
 * 2008-01-05, Manel Zaera (manelzaera@gmail.com), First version
 * 2008-01-10, Manel Zaera (manelzaera@gmail.com), Get valid values for a cell
 */
extract($_POST);
session_start();
require_once('SudokuMatrix.php');

$ajaxres = '';
if ($action=='new') {
	try {
		$levelcode = -1;
		if ($level=='easy') {
			$levelcode = SudokuMatrix::LEVEL_EASY;
		} elseif ($level=='medium') {
			$levelcode = SudokuMatrix::LEVEL_MEDIUM;
		} elseif ($level=='hard') {
			$levelcode = SudokuMatrix::LEVEL_HARD;
		} elseif ($level=='expert') {
			$levelcode = SudokuMatrix::LEVEL_EXPERT;
		}
		$matrixvalues = ($values=='')?null:explode(",",$values);
		$aSudoku = new SudokuMatrix($levelcode,$matrixvalues);
		$_SESSION['sudokumatrix'] = serialize($aSudoku);
		$ajaxres=implode(",",$aSudoku->getCells());
	} catch (Exception $aEx) {
		$ajaxres = 'Error: '.$aEx->getMessage();
	}
} elseif ($action=='set') {
	$aSudoku = unserialize($_SESSION['sudokumatrix']);
	if ($value == 0) {
		$ajaxres=$aSudoku->unsetCell($row,$col);
	} else {
		$ajaxres=$aSudoku->setCell($row,$col,$value);
	}
	$_SESSION['sudokumatrix'] = serialize($aSudoku);
} elseif ($action=='complete') {
	$aSudoku = unserialize($_SESSION['sudokumatrix']);
	$ajaxres=$aSudoku->complete();
} elseif ($action=='validv') {
	$aSudoku = unserialize($_SESSION['sudokumatrix']);
	$ajaxres=implode(",",$aSudoku->getValidValues($row,$col));
} else {
}
echo $ajaxres;
?>
